package com.example.cet4try.activity;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.example.cet4try.R;
import com.example.cet4try.Utils.Tools;
import com.example.cet4try.Utils.WordClass;
import com.example.cet4try.Utils.WordData;
import com.example.cet4try.adapter.WordListAdapter;
import com.example.cet4try.diyView.SlideRecyclerView;
import com.example.cet4try.diyView.TopView;
import com.example.cet4try.myDialog.WordDetailsDialog;

import java.util.List;

public class ActivityWordList extends BaiduTTSActivity {

    private SlideRecyclerView rv_sliding_delete;
    private List<WordClass> collectList;
    private WordListAdapter mInventoryAdapter;
    private WordDetailsDialog wordDetailsDialog;
    private WordData wordData;
    private String username,type;
    private TextView not_any;
    private View bar;
    private float barTouchedLastY;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_word_list);

        SharedPreferences preferences = getSharedPreferences("App_Shared_Data", 0);
        username = preferences.getString("username","");

        Intent intent = getIntent();
        type = intent.getStringExtra("type");
        if (type != null){
            String title = "";
            switch (type){
                case "learned":title = "已学习";break;
                case "already":title = "已学会";break;
                case "wrong":title = "错题记录";break;
                case "collect":title = "我的收藏";break;
                case "all":title = "复习单词";break;
                case "remain":title = "未学习";break;
                default:break;
            }
            TopView topView = findViewById(R.id.topview);
            topView.setmTitle(title);
        }

        wordDetailsDialog = new WordDetailsDialog("单词详情",true);
        wordDetailsDialog.setPlaySound(new WordDetailsDialog.PlaySound() {
            @Override
            public void speak_d() {
                speak(wordData.getEnglish());
            }
        });
        not_any = findViewById(R.id.not_any);

        bar = findViewById(R.id.scrollBar);
        rv_sliding_delete = (SlideRecyclerView) findViewById(R.id.rv_sliding_delete);
        rv_sliding_delete.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        if (type.equals("all")){
            rv_sliding_delete.setHaveDelete(false);
            collectList = Tools.GetDBData();
            wordDetailsDialog.setIsFuxi(true);
        }
        else if (type.equals("remain")){
            collectList = Tools.GetRemainDBData(username);
        }
        else {
            Tools.CheckUserData(username,"0_0",type);
            collectList = Tools.GetUserDatas(username,type);
        }
        ChekHaveData();

        mInventoryAdapter = new WordListAdapter(this, collectList);
        rv_sliding_delete.setAdapter(mInventoryAdapter);
        mInventoryAdapter.setOnDeleteClickListener(new WordListAdapter.OnDeleteClickLister() {
            @Override
            public void onDeleteClick(View view, int position) {//删除
                wordData = Tools.souEN(collectList.get(position).getEnglish());
                if (type.equals("remain"))
                    Tools.AddUserData(username,wordData.getMid(),"learned");
                else
                    Tools.DelUserData(username,wordData.getMid(),type);
                collectList.remove(position);
                mInventoryAdapter.notifyDataSetChanged();
                ChekHaveData();
                rv_sliding_delete.closeMenu();
            }
        });
        mInventoryAdapter.setOnClickListener(new WordListAdapter.OnClickListener() {//点击
            @Override
            public void onClick(View view, int position) {
                wordData = Tools.souEN(collectList.get(position).getEnglish());
                wordDetailsDialog.setData(wordData);
                wordDetailsDialog.show(getSupportFragmentManager(),"WordDetailsDialog");
                rv_sliding_delete.closeMenu();
            }
        });

        rv_sliding_delete.addOnScrollListener(new RecyclerView.OnScrollListener() {
              @Override
              public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                  super.onScrolled(recyclerView, dx, dy);
                  changeBarPosition();
              }
          });

        bar.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        barTouchedLastY = event.getRawY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        float dY = event.getRawY() - barTouchedLastY;
                        moveListView(dY);
//                        showToastView();
                        barTouchedLastY = event.getRawY();

                        //上下边界值
                        if (bar.getY() + dY + bar.getMeasuredHeight() >= rv_sliding_delete.getHeight()) {
                            //到底了
                            bar.setY(rv_sliding_delete.getHeight() - bar.getMeasuredHeight());
                        } else if (bar.getY() + dY <= 0) {
                            //到顶了
                            bar.setY(0);
                        } else {
                            bar.setY(bar.getY() + dY);
                        }
                        break;
                    case MotionEvent.ACTION_CANCEL:
                    case MotionEvent.ACTION_UP:
                        break;
                }

                return true;
            }
        });

    }

    private void moveListView(float dY) {

        //列表总长度
        int verticalScrollRange = rv_sliding_delete.computeVerticalScrollRange();
        //可见区域长度
        int verticalScrollExtent = rv_sliding_delete.computeVerticalScrollExtent();
        //向上滑动的大概距离
        int verticalScrollOffset = rv_sliding_delete.computeVerticalScrollOffset();

        int listMovableHeight = verticalScrollRange - verticalScrollExtent;
        int barMovableHeight = verticalScrollExtent - bar.getHeight();

        rv_sliding_delete.scrollBy(0, (int) (1.0f * listMovableHeight / barMovableHeight * dY));
    }
    private void changeBarPosition() {
        //列表总长度
        int verticalScrollRange = rv_sliding_delete.computeVerticalScrollRange();
        //可见区域长度
        int verticalScrollExtent = rv_sliding_delete.computeVerticalScrollExtent();
        //向上滑动的大概距离
        int verticalScrollOffset = rv_sliding_delete.computeVerticalScrollOffset();

        int listMovableHeight = verticalScrollRange - verticalScrollExtent;
        int barMovableHeight = verticalScrollExtent - bar.getHeight();
        bar.setY(1.0f * barMovableHeight / listMovableHeight * verticalScrollOffset);
    }

    private void ChekHaveData(){
        if (collectList == null||collectList.size() < 1){
            not_any.setVisibility(View.VISIBLE);
            rv_sliding_delete.setVisibility(View.GONE);
            bar.setVisibility(View.GONE);
        }
        else{
            not_any.setVisibility(View.GONE);
            rv_sliding_delete.setVisibility(View.VISIBLE);
            bar.setVisibility(View.VISIBLE);
        }

    }


}
